package com.devbobcorn.nekorization.entities;

import com.devbobcorn.nekorization.Nekorization;
import com.devbobcorn.nekorization.client.rendering.NekoRenderer;
import com.devbobcorn.nekorization.client.rendering.SpriteRenderer;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.common.ForgeSpawnEggItem;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.event.entity.EntityAttributeCreationEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fmllegacy.RegistryObject;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.IForgeRegistry;
import net.minecraft.client.renderer.entity.EntityRenderers;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.MobCategory;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;

@EventBusSubscriber(modid = Nekorization.MODID, bus = EventBusSubscriber.Bus.MOD)
public class ModEntityType {
	private static final Logger LOGGER = LogManager.getLogger("Mod Entity Type");

	public static final DeferredRegister<EntityType<?>> ENTITY_TYPES = DeferredRegister.create(ForgeRegistries.ENTITIES, Nekorization.MODID);

	public static final EntityType<NekoEntity> NEKO_TYPE = EntityType.Builder.<NekoEntity>of(NekoEntity::new, MobCategory.CREATURE).sized(0.6F, 1.8F).clientTrackingRange(8).build(Nekorization.MODID + ":neko");
	public static final RegistryObject<EntityType<NekoEntity>> $NEKO_TYPE = ENTITY_TYPES.register("neko", () -> NEKO_TYPE);

	public static final EntityType<SpriteEntity> SPRITE_TYPE = EntityType.Builder.<SpriteEntity>of(SpriteEntity::new, MobCategory.CREATURE).sized(0.4F, 0.4F).clientTrackingRange(8).build(Nekorization.MODID + ":sprite");
	public static final RegistryObject<EntityType<SpriteEntity>> $SPRITE_TYPE = ENTITY_TYPES.register("sprite", () -> SPRITE_TYPE);

	@SubscribeEvent
	public static void registerAttributes(EntityAttributeCreationEvent event) {
		event.put(NEKO_TYPE, Mob.createMobAttributes().add(Attributes.MOVEMENT_SPEED, 0.3).add(Attributes.MAX_HEALTH, 10).add(Attributes.ARMOR, 0).add(Attributes.ATTACK_DAMAGE, 3).build());
		event.put(SPRITE_TYPE, Mob.createMobAttributes().add(Attributes.FLYING_SPEED, 0.4).add(Attributes.MOVEMENT_SPEED, 0.2).add(Attributes.MAX_HEALTH, 4).add(Attributes.ARMOR, 0).build());

		LOGGER.info("Entity Attributes Registered.");
	}

	@SubscribeEvent
	@OnlyIn(Dist.CLIENT)
	public static void registerModels(ModelRegistryEvent event) {
		EntityRenderers.register(NEKO_TYPE, (ctx) -> new NekoRenderer(ctx, true));
		EntityRenderers.register(SPRITE_TYPE, (ctx) -> new SpriteRenderer(ctx));

		LOGGER.info("Entity Renderers Registered.");
	}

	public static final Item NEKO_SPAWN_EGG = new ForgeSpawnEggItem($NEKO_TYPE, -327937, -8257543, new Item.Properties().tab(CreativeModeTab.TAB_MISC)).setRegistryName("neko_spawn_egg");
	public static final Item SPRITE_SPAWN_EGG = new ForgeSpawnEggItem($SPRITE_TYPE, -327937, -8257543, new Item.Properties().tab(CreativeModeTab.TAB_MISC)).setRegistryName("sprite_spawn_egg");

	@SubscribeEvent
	public static void registerSpawnEggs(final RegistryEvent.Register<Item> event) {
		IForgeRegistry<Item> registry = event.getRegistry();

		registry.register(NEKO_SPAWN_EGG);
		registry.register(SPRITE_SPAWN_EGG);
	}
}
